﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;

namespace JXT.PrimaryKey.Batman.Core.Configuration
{
    /// <summary>
    /// 交易所配置集合
    /// </summary>
    public class ExchangeSettingConfigurationElementCollection : ConfigurationElementCollection, IEnumerable<ExchangeConfigurationElement>
    {
        [ConfigurationProperty("defaultOpenAM", IsRequired = true)]
        public TimeSpan DefaultOpenAM
        {
            get { return (TimeSpan)base["defaultOpenAM"]; }
            set { base["defaultOpenAM"] = value; }
        }

        [ConfigurationProperty("defaultCloseAM", IsRequired = true)]
        public TimeSpan DefaultCloseAM
        {
            get { return (TimeSpan)base["defaultCloseAM"]; }
            set { base["defaultCloseAM"] = value; }
        }

        [ConfigurationProperty("defaultOpenPM", IsRequired = true)]
        public TimeSpan DefaultOpenPM
        {
            get { return (TimeSpan)base["defaultOpenPM"]; }
            set { base["defaultOpenPM"] = value; }
        }

        [ConfigurationProperty("defaultClosePM", IsRequired = true)]
        public TimeSpan DefaultClosePM
        {
            get { return (TimeSpan)base["defaultClosePM"]; }
            set { base["defaultClosePM"] = value; }
        }

        /// <summary>
        /// 默认延迟时间(0秒)
        /// </summary>
        [ConfigurationProperty("defaultDelay", IsRequired = false, DefaultValue = 0)]
        public int DefaultDelay
        {
            get { return (int)base["defaultDelay"]; }
            set { base["defaultDelay"] = value; }
        }

        public new ExchangeConfigurationElement this[string name]
        {
            get
            {
                return BaseGet(name) as ExchangeConfigurationElement;
            }
            set
            {
                if (BaseGet(name) != null)
                {
                    base.BaseRemove(name);
                }
                BaseAdd(value);
            }
        }

        protected override ConfigurationElement CreateNewElement()
        {
            return new ExchangeConfigurationElement();
        }

        protected override object GetElementKey(ConfigurationElement element)
        {
            return ((ExchangeConfigurationElement)element).Name;
        }

        public new IEnumerator<ExchangeConfigurationElement> GetEnumerator()
        {
            for (int i = 0; i < Count; i++)
            {
                yield return (ExchangeConfigurationElement)BaseGet(i);
            }
        }
    }
}
